﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Shared.ApiVersioningHelper
{
    public static class ApiVersioningExtensions
    {
        /// <summary>
        /// 添加版本控制
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddApiVersioningExt(this IServiceCollection services)
        { 
            return services.AddApiVersioning(option =>
            {
                option.ReportApiVersions = true; //可选,为true时 API 返回支持的版本信息
                //不提供版本时,默认 1.0 
                option.AssumeDefaultVersionWhenUnspecified = true;
                option.ReportApiVersions = true;
                //请求中未指定版本时,默认1.0
                option.DefaultApiVersion = new ApiVersion(1, 0);
            }).AddVersionedApiExplorer(option =>
            {
                //版本名的格式: v + 版本号
                option.GroupNameFormat = "'v'VVVV";
                option.AssumeDefaultVersionWhenUnspecified = true;
            });
        }
    }
}
